/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2016
 */

package com.jzwl.site.manager.carouselv2.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.jzwl.common.constant.AjaxStatusConstant;
import com.jzwl.common.page.PageObject;
import com.jzwl.common.upload.service.UploadImgService;
import com.jzwl.site.manager.carouselv2.service.SiteCarouselService;
import com.jzwl.system.base.controller.BaseController;

/**
 * SiteCarousel
 * SiteCarousel
 * siteCarousel
 * <p>Title:SiteCarouselController </p>
 * 	<p>Description:SiteCarousel </p>
 * 	<p>Company: </p> 
 * 	@author aotu-code 
 */
@Controller
@RequestMapping("/siteCarousel")
public class SiteCarouselController extends BaseController {

	@Autowired
	private SiteCarouselService siteCarouselService;
	@Autowired
	private UploadImgService uploadImgService;

	/**
	 * 跳转到添加SiteCarousel的页面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/toAddSiteCarousel")
	public ModelAndView toAddsiteCarousel(HttpServletRequest request, HttpServletResponse response) {

		ModelAndView mov = new ModelAndView();

		createParameterMap(request);

		mov.setViewName("/manager/carouse/add");
		return mov;
	}

	/**
	 * 添加SiteCarousel
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/addSiteCarousel")
	public ModelAndView addsiteCarousel(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		ModelAndView mov = new ModelAndView();

		createParameterMap(request);

		try {
			boolean flag = siteCarouselService.addSiteCarousel(paramsMap);
			if (!flag) {
				mov.addObject("msg", "添加SiteCarousel失败!");
				mov.setViewName("/error");
			} else {
				mov.setViewName("redirect:/siteCarousel/siteCarouselList.html");
			}
		} catch (Exception e) {
			e.printStackTrace();
			mov.addObject("msg", "添加SiteCarousel失败!");
			mov.setViewName("/error");
		}

		return mov;
	}

	/**
	 * siteCarousellist
	 */
	@RequestMapping(value = "/siteCarouselList")
	public ModelAndView siteCarouselList(HttpServletRequest request, HttpServletResponse response) {

		ModelAndView mov = new ModelAndView();

		createParameterMap(request);

		PageObject po = new PageObject();

		try {

			po = siteCarouselService.querySiteCarouselList(paramsMap);

			mov.addObject("list", po.getDatasource());
			mov.addObject("totalProperty", po.getTotalCount());
			mov.addObject("po", po);
			mov.addObject("paramsMap", paramsMap);
			mov.setViewName("/manager/carouse/list");
		} catch (Exception e) {

			e.printStackTrace();
			mov.addObject("msg", "siteCarousel查询失败");
			mov.setViewName("/error");
		}

		return mov;
	}

	/**
	 * 跳转到修改siteCarousel页面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/toUpdateSiteCarousel")
	public ModelAndView toUpdateSiteCarousel(HttpServletRequest request, HttpServletResponse response) {

		ModelAndView mov = new ModelAndView();

		createParameterMap(request);

		try {
			Map<String, Object> siteCarousel = siteCarouselService.getById(paramsMap);
			mov.addObject("obj", siteCarousel);
			mov.setViewName("/manager/carouse/update");
		} catch (Exception e) {
			e.printStackTrace();
			mov.addObject("msg", "siteCarousel查询失败");
			mov.setViewName("/error");
		}

		return mov;
	}

	/**
	 * 跳转到详情siteCarousel页面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/detailSiteCarousel")
	public ModelAndView detailSiteCarousel(HttpServletRequest request, HttpServletResponse response) {

		ModelAndView mov = new ModelAndView();

		createParameterMap(request);

		try {
			Map<String, Object> siteCarousel = siteCarouselService.getById(paramsMap);
			mov.addObject("obj", siteCarousel);
			mov.setViewName("/manager/carouse/detail");
		} catch (Exception e) {
			e.printStackTrace();
			mov.addObject("msg", "siteCarousel查询失败");
			mov.setViewName("/error");
		}

		return mov;
	}

	/**
	 * 修改siteCarousel
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/updateSiteCarousel")
	public ModelAndView updateSiteCarousel(HttpServletRequest request, HttpServletResponse response) {

		ModelAndView mov = new ModelAndView();

		createParameterMap(request);

		try {

			boolean flag = siteCarouselService.updateSiteCarousel(paramsMap);

			if (!flag) {
				mov.addObject("msg", "修改失败!");
				mov.setViewName("/error");
			} else {
				mov.setViewName("redirect:/siteCarousel/siteCarouselList.html");
			}
		} catch (Exception e) {

			e.printStackTrace();
			mov.addObject("msg", "修改失败!");
			mov.setViewName("/error");
		}

		return mov;
	}

	/**
	 * 逻辑删除siteCarousel
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/delSiteCarousel")
	public ModelAndView delSiteCarousel(HttpServletRequest request, HttpServletResponse response) {

		ModelAndView mov = new ModelAndView();

		createParameterMap(request);

		try {

			boolean flag = siteCarouselService.deleteSiteCarousel(paramsMap);

			if (!flag) {
				mov.addObject("msg", "删除失败!");
				mov.setViewName("/error");
			} else {
				mov.setViewName("redirect:/siteCarousel/siteCarouselList.html");
			}
		} catch (Exception e) {

			e.printStackTrace();
			mov.addObject("msg", "删除失败!");
			mov.setViewName("/error");

		}

		return mov;
	}

	/**
	 * 物理删除siteCarousel
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/deleteSiteCarousel")
	public ModelAndView deleteSiteCarousel(HttpServletRequest request, HttpServletResponse response) {

		ModelAndView mov = new ModelAndView();

		createParameterMap(request);

		try {

			boolean flag = siteCarouselService.deleteSiteCarousel(paramsMap);

			if (!flag) {
				mov.addObject("msg", "删除失败!");
				mov.setViewName("/error");
			} else {
				mov.setViewName("redirect:/siteCarousel/siteCarouselList.html");
			}
		} catch (Exception e) {

			e.printStackTrace();
			mov.addObject("msg", "删除失败!");
			mov.setViewName("/error");

		}

		return mov;
	}

	/**
	 * 
	 * 项目名称：auto-code-v5
	 * 描述：异步上传图片，单张
	 * 创建人： ln
	 * 创建时间： 2016年9月6日
	 * 标记：manager
	 * @param img
	 * @param request
	 * @param response
	 * @return
	 * @version
	 * para1:type表示上传到某一个资源上去1：表示静态资源，2：表示上传到oss服务器上去
	 * para2:img 要上传的图片
	 * para3:flag 当type 为2时 flag表示是那个项目的图片eg：bbs，tg,zb..当为1时可传null,
	 */
	@RequestMapping(value = "uploadImg", method = RequestMethod.POST)
	public @ResponseBody Map uploadImg(@RequestParam(required = false) MultipartFile pic, HttpServletRequest request,
			HttpServletResponse response) {
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			int type = 1;
			String url = uploadImgService.uploadImg(type, pic);//返回全路径
			/*uploadImgService.uploadImg(int 2,img,"bbs");*/
			map.put("path", url);
			map.put("url", url);
			map.put("ajax_status", AjaxStatusConstant.AJAX_STATUS_SUCCESS);
			return map;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	//auto-code-end

}
